/** IBRPG Main **/
YAHOO.namespace('alw.ibrpg');
var yud=YAHOO.util.Dom,yue=YAHOO.util.Event;

YAHOO.alw.ibrpg = {
  globals: {
    version: '0.55',
    domain: 'ittybittyrpg.com',
    panels:{
      main_panel: null,
      alert_panel: null,
      splash: null
    },
    worlds:[],
    character: {
		name: "",
		chclass: "",
		experience:0,
		level:0,
		loot: [
			{name:'gold',amt:0}
			],
		equipment:[
				{name:'clothing', defense: 0, power: 0}
			],
		victories: [],
		health:10,
		power:1,
		defense:1
		},
	storageEnabled: false,
	storageMirror: null,
	battleStats:{}
  },
  commands:{
    spawnMonster: function(mon,posArr){
        var wrld = YAHOO.alw.ibrpg.globals.worlds[0]
        var monImg = wrld.monsters[4].img;//hard coded right now - should be mon
        var page = document.getElementsByTagName('body')[0]
        var monEl = "<div class='rndMonster' style='cursor:pointer;position:absolute;left:"+posArr[0]+"px;top:"+posArr[1]+"px;' onclick='alert(\"we will fight someday!\")'><img src='"+monImg+"'/></div>";
        var monElWrp = document.createElement('div');//remove this cludge
        monElWrp.innerHTML = monEl;
        //page.innerHTML+=monEl;
        page.appendChild(monElWrp);
        },
    rndMove: function(){
    	var monsters = yud.getElementsByClassName('rndMonster');
    	for(mm =0; mm < monsters.length; mm++){
    		if(Math.floor(Math.random() * 100) < 60){
    			var anim = new YAHOO.util.Motion(monsters[mm], { points: { by: [(Math.floor(Math.random() *20) -10), (Math.floor(Math.random() *20) -10)] } });
    			anim.animate();
    		}
    	}
    	window.setTimeout('YAHOO.alw.ibrpg.commands.rndMove()',3000);
    }
  },
  init: function(){
    try{console.log('init called!');} catch(e){}
    yud.get('testimg').setAttribute('src',YAHOO.alw.ibrpg.globals.worlds[0].monsters[2].img);
    yud.get('testdiv').innerHTML = YAHOO.alw.ibrpg.globals.worlds[0].dialogs.intro.text;
    
    if(window.globalStorage){
    	YAHOO.alw.ibrpg.globals.storageEnabled = true;
    }
    
    
    //initialize panels
    this.globals.main_panel = new YAHOO.widget.Panel("ibrpg_mainpanel",
     		{ 
	    		close:true,  
	    		visible:false,  
	    		draggable:true,
	    		modal: true,
	    		constraintoviewport:true,
	    		effect:{effect:YAHOO.widget.ContainerEffect.FADE, duration: .5},
	    		xy: [100,100]
			} 
		);
	this.globals.alert_panel = new YAHOO.widget.Panel('ibrpg_alertpanel',
			{
				close: true,
				visible: false,
				width: "200px",
				xy: [-200,-200]
			
			}
		
		);
	//get or set localstorage...	
		
		
    this.doIt();
  },
  
  getStorage: function(){
		//return false if storage not possible.
		if(window.globalStorage){
			var storage = globalStorage[this.globals.domain];//change to your domain
			if(storage.ibrpgtest && storage.ibrpgtest != ''){
				//console.log(storage.ibrpgtest)
				this.globals.storageMirror = storage.ibrpgtest.value.parseJSON();
				//console.log("storageMirror" )
				//console.log(this.globals.storageMirror);
				this.globals.registered = true;//safe assumption?
				return true;
			}else {
				//initialize storage
				storage.ibrpgtest = this.globals.character.toJSONString();
				this.globals.storageMirror = storage.ibrpgtest.value.parseJSON();
				//console.log(storage.ibrpgtest);
				return true;
			}
		}else{
			return false;
		}
	
	},
  
  putStorage: function(){
		if(window.globalStorage){
			var storage = globalStorage[this.globals.domain];//change to your domain
			storage.ibrpgtest = YAHOO.alw.ibrpg.globals.storageMirror.toJSONString();
			return true;
		}else{
			return false;
		}
	},
  
  trace: function(msg){
  	try{console.log(msg);} catch(e){}
  },
  doIt: function(){
    yud.get('ibrpgstart').innerHTML = '';
    var oButton = new YAHOO.widget.Button({ 
	   id: "ibrpg_internal_start",  
	   type: "button",  
	   label: "Start IBRPG",  
	   container: "ibrpgstart",
       onclick: {
        fn:this.splash
       	}   
	   });
    //this.splash() 
  },
  splash: function(){
    var spl = document.createElement('div');
    spl.setAttribute('id','ibrpg_splash_window');
    spl.innerHTML="<h1>Itty Bitty RPG</h1><div id='ibrpg_splash_toolbar'><blink>Loading...</blink></div>";
    yud.setStyle(spl,'opacity',0);
    document.getElementsByTagName('body')[0].appendChild(spl);
    
    
    
    
    var anim = new YAHOO.util.Anim(
        spl, 
        { opacity: { to: 1 } }, 
        2, 
        YAHOO.util.Easing.easeIn
    );
    
    
    anim.onComplete.subscribe(YAHOO.alw.ibrpg.startDialog);
    anim.onComplete.subscribe(YAHOO.alw.ibrpg.unSplash);
    anim.animate();
    
  },
  unSplash: function(){
  	var spl = document.getElementById('ibrpg_splash_window');
  	spl.innerHTML = '';
  },
  progressPanel: function(dest){
    //alert('progress panel with' + dest);
    this.trace(' - ProgressPanel Called!');
    var pnl = YAHOO.alw.ibrpg.globals.main_panel;
    var wrld = YAHOO.alw.ibrpg.globals.worlds[0];
    if(!wrld.dialogs[dest]){
        return false;
    }
    //get face url
    var face = null;
    for(fc=0; fc < wrld.faces.length; fc++){
        if (wrld.faces[fc].name == wrld.dialogs[dest].face){
            //alert('have a match!');
            face =wrld.faces[fc].img;
        }
    }
    //get dialog
    var dialog = wrld.dialogs[dest].text;
    //get links
    var links='';
    for(lnk=0; lnk < wrld.dialogs[dest].next.length; lnk++){
        links+="[ <a href='javascript:void(0)' onclick='YAHOO.alw.ibrpg.progressPanel(\""+wrld.dialogs[dest].next[lnk].to+"\"); return false;'>"+wrld.dialogs[dest].next[lnk].label+"</a>] &nbsp;";
    }
    pnlText = "<img src="+face+">";
    pnlText += "<div class='ibrpg_dlg'>"+dialog+"</div>";
    pnlText += "<div class=''>"+links+"</div>";
    pnl.setBody(pnlText);
    pnl.render(document.body);
    
    
    if(wrld.dialogs[dest].commands && wrld.dialogs[dest].commands.length > 0){
    	var wCommands = wrld.dialogs[dest].commands;
    	for(cmd = 0; cmd < wCommands.length; cmd++){
    		eval("YAHOO.alw.ibrpg.commands."+wrld.dialogs[dest].commands[cmd].label+"("+wrld.dialogs[dest].commands[cmd].args+")")
    	}
    }
    
    return false;
  },
  startDialog: function(){
    YAHOO.alw.ibrpg.trace(' - StartDialog Called! - ');
    var pnl = YAHOO.alw.ibrpg.globals.main_panel;
    var pnlText = "<img src="+YAHOO.alw.ibrpg.globals.worlds[0].faces[0].img+">";
    pnlText+="<div class='ibrpg_dlg'>"+YAHOO.alw.ibrpg.globals.worlds[0].dialogs.intro.text+"</div>";
    pnlText+="<div class='ibrpg_controls'>";
        for(k = 0; k < YAHOO.alw.ibrpg.globals.worlds[0].dialogs.intro.next.length ; k++) {
            pnlText +="[ <a href='' onclick='YAHOO.alw.ibrpg.progressPanel(\""+YAHOO.alw.ibrpg.globals.worlds[0].dialogs.intro.next[k].to+"\"); return false;'>"+YAHOO.alw.ibrpg.globals.worlds[0].dialogs.intro.next[k].label+"</a> ] &nbsp;";
        }
    pnlText+="</div>";
    pnl.setBody(pnlText);
    pnl.render(document.body);
    pnl.show()
  }
}


yue.on(window, 'load',YAHOO.alw.ibrpg.init,YAHOO.alw.ibrpg, true);
